import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";

export default {
  namespace: 'goodItem',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      const p = {
        ...payload,
        baseUrl: getBaseUrl(),
        process: (records) => {
          return records.map(record => {
            const {originalPrice, price} = record;
            return {...record, originalPrice: originalPrice / 100, price: price / 100}
          });
        }
      }

      yield put({
        type: 'baseGetPageInfo',
        payload: p,
      });
    },

    // 保存和修改
    * save({payload}, {put}) {

      const {value} = payload;
      const {originalPrice, price} = value;
      payload.value = {...value, originalPrice: originalPrice * 100, price: price * 100};

      yield put({
        type: 'baseSave',
        payload: {...payload, baseUrl: getBaseUrl()}
      });
    },


    // 删除 之后查询分页
    * delete({payload}, {call, put, select}) {

      yield put({
        type: 'baseDelete',
        payload: {...payload, baseUrl: getBaseUrl()}
      });
    },

    * getAll({payload}, {put, call}) {

      yield put({
        type: 'baseGetAll',
        payload: {...payload, baseUrl: getBaseUrl()}
      });
    }
  }

}

function getBaseUrl() {
  return "/goodItem";
}

